package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"medical/models"
	"strconv"
	"time"
)

type MedicalController struct {
	beego.Controller
}

func (c *MedicalController) Get() {

}

// 添加
func (c *MedicalController) Add() {
	name := c.GetString("name")
	age, _ := c.GetInt("age")
	sex := c.GetString("sex")
	tel := c.GetString("tel")
	if name == "" || age <= 0 || sex == "" || tel == "" {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "数据不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	madical := models.Medical{
		Name: name,
		Age:  age,
		Sex:  sex,
		Tel:  tel,
	}
	err := models.DB.Create(&madical).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

// 列表
func (c *MedicalController) List() {
	pageDate := make(map[string]interface{})
	var medical []models.Medical
	query := models.DB.Model(&models.Medical{})
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	name := c.GetString("name")
	key := "page-" + strconv.Itoa(page) + "-" + name
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}

	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		var count int64
		query.Count(&count)
		offset := (page - 1) * size
		err = query.Offset(offset).Limit(size).Find(&medical).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			pageDate["count"] = count
			pageDate["medical"] = medical
			jsonbyte, _ := json.Marshal(&pageDate)
			value := string(jsonbyte)
			models.Rdb.Set(key, value, time.Minute*3)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "mysql-查询成功",
				Data: pageDate,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageDate)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "redis-查询成功",
			Data: pageDate,
		}
		c.ServeJSON()
	}

}

// 详情
func (c *MedicalController) Info() {
	id, _ := c.GetInt("id")
	var medical models.Medical
	key := "medical-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	Rankkey := "rank-medical"
	if err != nil {
		err = models.DB.Where("id=?", id).First(&medical).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "详情查询失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			models.Rdb.ZIncrBy(Rankkey, 1, medical.Name)
			jsonbyte, _ := json.Marshal(&medical)
			value := string(jsonbyte)
			models.Rdb.Set(key, value, time.Minute*3)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "mysql-详情查询成功",
				Data: medical,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &medical)
		models.Rdb.ZIncrBy(Rankkey, 1, medical.Name)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "redis-详情查询成功",
			Data: medical,
		}
		c.ServeJSON()
	}
}

func (c *MedicalController) Phb() {
	key := "rank-medical"
	nums := models.Rdb.ZCard(key).Val()
	ranks, err := models.Rdb.ZRevRangeWithScores(key, 0, nums-1).Result()
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "展示失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "展示成功",
			Data: ranks,
		}
		c.ServeJSON()
	}
}
